package com.example.administrator.smarthome_androids;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.com.sky.downloader.greendao.OpenBeanDao;

import java.util.List;

public class DBActivity extends AppCompatActivity implements View.OnClickListener {
    protected ListView mLv;
    protected EditText mAddEdit;
    protected Button mBtn1;
    protected Button mBtn2;
    protected Button mBtn3;
    protected EditText mAddEditname;
    protected EditText mAddEditroom;
    protected EditText mAddOnOff;
    protected Button mBtn4;
    private String mAddoffText;
    private String mAddEditroomText;
    private String mAddEditnameText;

String TAG="GGG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_db);
        initView();

    }


    private void initView() {
        mAddEdit = (EditText) findViewById(R.id.add_edit);
        mBtn1 = (Button) findViewById(R.id.btn1);
        mBtn1.setOnClickListener(DBActivity.this);
        mBtn2 = (Button) findViewById(R.id.btn2);
        mBtn2.setOnClickListener(DBActivity.this);
        mBtn3 = (Button) findViewById(R.id.btn3);
        mBtn3.setOnClickListener(DBActivity.this);
        mAddEditname = (EditText) findViewById(R.id.add_editname);
        mAddEditroom = (EditText) findViewById(R.id.add_editroom);
        mAddOnOff = (EditText) findViewById(R.id.add_on_off);
        mBtn4 = (Button) findViewById(R.id.btn4);
        mBtn4.setOnClickListener(DBActivity.this);
        mAddEditname.addTextChangedListener(new mytextwatcher());
        mAddEditroom.addTextChangedListener(new mytextwatcher());
        mAddOnOff.addTextChangedListener(new mytextwatcher());
    }

    public  class mytextwatcher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //只有三个输入框都填入信息时才让用户添加配置设备信息
if (mAddEditroom.getText().length()>0&&mAddEditname.getText().length()>0&&mAddOnOff.getText().length()>0){
    mAddEditnameText =  mAddEditname.getText().toString();
    mAddEditroomText =   mAddEditroom.getText().toString();
    mAddoffText =   mAddOnOff.getText().toString();
}
        }
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn1) {
            addone();

        } else if (view.getId() == R.id.btn2) {
            serachs();
        } else if (view.getId() == R.id.btn3) {
//当监听到用户操作app录入设备信息或者更改信息时，通过socket给A33发送修改后的数据，通知A33修改保存的信息
            Log.e(TAG,mAddEditnameText);
            Log.e(TAG,mAddEditroomText);
            Log.e(TAG,mAddoffText);
            DeviceInfo  info =new DeviceInfo(null,mAddEditnameText,mAddEditroomText,mAddoffText);
            DBManager.getDaoSession(this).getDeviceInfoDao().insert(info);
            //发送数据给A33


        } else if (view.getId() == R.id.btn4) {
            //查询保存后的数据
            List<DeviceInfo> list = DBManager.getDaoSession(this).getDeviceInfoDao().queryBuilder().list();
            Log.e(TAG,list+"");

        }
    }

    //搜索数据
    private void serach() {
        String s = mAddEdit.getText().toString();
        List<OpenBean> openBeen = DBUtils.queryByStr(this, s);
        Log.e("kkk", openBeen + "");
        Toast.makeText(this, "您搜索的数据是：" + openBeen, Toast.LENGTH_SHORT).show();

    }


    private void serachs() {
        String s = mAddEdit.getText().toString();
        List<OpenBean> openBeen = DBManager.getDaoSession(DBActivity.this).getOpenBeanDao().queryBuilder()
                .where(OpenBeanDao.Properties.MString.eq("light_smartHome")).where(OpenBeanDao.Properties.Type.eq("开"))
                .where(OpenBeanDao.Properties.Location.eq("主卧")).list();

        Log.e("ssss", openBeen + "");
        Toast.makeText(this, "您搜索的数据是：" + openBeen, Toast.LENGTH_SHORT).show();

    }


    //添加数据
    private void addone() {

        OpenBean openBean1 = new OpenBean(null, "light_smartHome", "20:00:00", "开", "主卧", "27");
        OpenBean openBean2 = new OpenBean(null, "light_smartHome", "20:00:00", "关", "主卧", "28");
        OpenBean openBean3 = new OpenBean(null, "airControl_smartHome", "21:00:00", "开", "主卧", "25");
        OpenBean openBean4 = new OpenBean(null, "airControl_smartHome", "23:00:00", "关", "主卧", "27");
        OpenBean openBean5 = new OpenBean(null, "light_smartHome", "19:00:00", "开", "次卧", "27");
        OpenBean openBean6 = new OpenBean(null, "light_smartHome", "20:00:00", "关", "次卧", "27");
        OpenBean openBean7 = new OpenBean(null, "airControl_smartHome", "21:00：00", "开", "次卧", "29");
        OpenBean openBean8 = new OpenBean(null, "airControl_smartHome", "22:00：00", "关", "次卧", "26");


        DBUtils.insertData(this, openBean1);
        DBUtils.insertData(this, openBean2);
        DBUtils.insertData(this, openBean3);
        DBUtils.insertData(this, openBean4);
        // String s = mAddEdit.getText().toString();

        DBUtils.insertData(this, openBean5);
        DBUtils.insertData(this, openBean6);
        DBUtils.insertData(this, openBean7);
        DBUtils.insertData(this, openBean8);

    }
}











